﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace Module_Debug.DDIC_Tab
{
    public partial class Form_NT37700C_Gamma : Form
    {
        public Form_NT37700C_Gamma()
        {
            InitializeComponent();
        }

        private void Form_NT37700C_Gamma_Load(object sender, EventArgs e)
        {

        }

        private int[] Parttern_NT37700C = new int[34] { 255, 247, 239, 231, 223, 215, 207, 199, 191, 183, 175, 159, 143, 127, 111, 95, 79, 63, 55, 47, 39, 31, 23, 19, 17, 15, 13, 11, 9, 7, 5, 3, 1, 0 };
        private void btnCalculateNT37700C_Click(object sender, EventArgs e)
        {
            txtNT_33.Text = (Math.Pow((Parttern_NT37700C[0] / 255.0), Convert.ToDouble(txtNT_gamma.Text)) * Convert.ToDouble(txtNT_lum.Text)).ToString("F4");
            txtNT_32.Text = (Math.Pow((Parttern_NT37700C[1] / 255.0), Convert.ToDouble(txtNT_gamma.Text)) * Convert.ToDouble(txtNT_lum.Text)).ToString("F4");
            txtNT_31.Text = (Math.Pow((Parttern_NT37700C[2] / 255.0), Convert.ToDouble(txtNT_gamma.Text)) * Convert.ToDouble(txtNT_lum.Text)).ToString("F4");
            txtNT_30.Text = (Math.Pow((Parttern_NT37700C[3] / 255.0), Convert.ToDouble(txtNT_gamma.Text)) * Convert.ToDouble(txtNT_lum.Text)).ToString("F4");
            txtNT_29.Text = (Math.Pow((Parttern_NT37700C[4] / 255.0), Convert.ToDouble(txtNT_gamma.Text)) * Convert.ToDouble(txtNT_lum.Text)).ToString("F4");
            txtNT_28.Text = (Math.Pow((Parttern_NT37700C[5] / 255.0), Convert.ToDouble(txtNT_gamma.Text)) * Convert.ToDouble(txtNT_lum.Text)).ToString("F4");
            txtNT_27.Text = (Math.Pow((Parttern_NT37700C[6] / 255.0), Convert.ToDouble(txtNT_gamma.Text)) * Convert.ToDouble(txtNT_lum.Text)).ToString("F4");
            txtNT_26.Text = (Math.Pow((Parttern_NT37700C[7] / 255.0), Convert.ToDouble(txtNT_gamma.Text)) * Convert.ToDouble(txtNT_lum.Text)).ToString("F4");
            txtNT_25.Text = (Math.Pow((Parttern_NT37700C[8] / 255.0), Convert.ToDouble(txtNT_gamma.Text)) * Convert.ToDouble(txtNT_lum.Text)).ToString("F4");
            txtNT_24.Text = (Math.Pow((Parttern_NT37700C[9] / 255.0), Convert.ToDouble(txtNT_gamma.Text)) * Convert.ToDouble(txtNT_lum.Text)).ToString("F4");
            txtNT_23.Text = (Math.Pow((Parttern_NT37700C[10] / 255.0), Convert.ToDouble(txtNT_gamma.Text)) * Convert.ToDouble(txtNT_lum.Text)).ToString("F4");
            txtNT_22.Text = (Math.Pow((Parttern_NT37700C[11] / 255.0), Convert.ToDouble(txtNT_gamma.Text)) * Convert.ToDouble(txtNT_lum.Text)).ToString("F4");
            txtNT_21.Text = (Math.Pow((Parttern_NT37700C[12] / 255.0), Convert.ToDouble(txtNT_gamma.Text)) * Convert.ToDouble(txtNT_lum.Text)).ToString("F4");
            txtNT_20.Text = (Math.Pow((Parttern_NT37700C[13] / 255.0), Convert.ToDouble(txtNT_gamma.Text)) * Convert.ToDouble(txtNT_lum.Text)).ToString("F4");
            txtNT_19.Text = (Math.Pow((Parttern_NT37700C[14] / 255.0), Convert.ToDouble(txtNT_gamma.Text)) * Convert.ToDouble(txtNT_lum.Text)).ToString("F4");
            txtNT_18.Text = (Math.Pow((Parttern_NT37700C[15] / 255.0), Convert.ToDouble(txtNT_gamma.Text)) * Convert.ToDouble(txtNT_lum.Text)).ToString("F4");
            txtNT_17.Text = (Math.Pow((Parttern_NT37700C[16] / 255.0), Convert.ToDouble(txtNT_gamma.Text)) * Convert.ToDouble(txtNT_lum.Text)).ToString("F4");
            txtNT_16.Text = (Math.Pow((Parttern_NT37700C[17] / 255.0), Convert.ToDouble(txtNT_gamma.Text)) * Convert.ToDouble(txtNT_lum.Text)).ToString("F4");
            txtNT_15.Text = (Math.Pow((Parttern_NT37700C[18] / 255.0), Convert.ToDouble(txtNT_gamma.Text)) * Convert.ToDouble(txtNT_lum.Text)).ToString("F4");
            txtNT_14.Text = (Math.Pow((Parttern_NT37700C[19] / 255.0), Convert.ToDouble(txtNT_gamma.Text)) * Convert.ToDouble(txtNT_lum.Text)).ToString("F4");
            txtNT_13.Text = (Math.Pow((Parttern_NT37700C[20] / 255.0), Convert.ToDouble(txtNT_gamma.Text)) * Convert.ToDouble(txtNT_lum.Text)).ToString("F4");
            txtNT_12.Text = (Math.Pow((Parttern_NT37700C[21] / 255.0), Convert.ToDouble(txtNT_gamma.Text)) * Convert.ToDouble(txtNT_lum.Text)).ToString("F4");
            txtNT_11.Text = (Math.Pow((Parttern_NT37700C[22] / 255.0), Convert.ToDouble(txtNT_gamma.Text)) * Convert.ToDouble(txtNT_lum.Text)).ToString("F4");
            txtNT_10.Text = (Math.Pow((Parttern_NT37700C[23] / 255.0), Convert.ToDouble(txtNT_gamma.Text)) * Convert.ToDouble(txtNT_lum.Text)).ToString("F4");
            txtNT_9.Text = (Math.Pow((Parttern_NT37700C[24] / 255.0), Convert.ToDouble(txtNT_gamma.Text)) * Convert.ToDouble(txtNT_lum.Text)).ToString("F4");
            txtNT_8.Text = (Math.Pow((Parttern_NT37700C[25] / 255.0), Convert.ToDouble(txtNT_gamma.Text)) * Convert.ToDouble(txtNT_lum.Text)).ToString("F4");
            txtNT_7.Text = (Math.Pow((Parttern_NT37700C[26] / 255.0), Convert.ToDouble(txtNT_gamma.Text)) * Convert.ToDouble(txtNT_lum.Text)).ToString("F4");
            txtNT_6.Text = (Math.Pow((Parttern_NT37700C[27] / 255.0), Convert.ToDouble(txtNT_gamma.Text)) * Convert.ToDouble(txtNT_lum.Text)).ToString("F4");
            txtNT_5.Text = (Math.Pow((Parttern_NT37700C[28] / 255.0), Convert.ToDouble(txtNT_gamma.Text)) * Convert.ToDouble(txtNT_lum.Text)).ToString("F4");
            txtNT_4.Text = (Math.Pow((Parttern_NT37700C[29] / 255.0), Convert.ToDouble(txtNT_gamma.Text)) * Convert.ToDouble(txtNT_lum.Text)).ToString("F4");
            txtNT_3.Text = (Math.Pow((Parttern_NT37700C[30] / 255.0), Convert.ToDouble(txtNT_gamma.Text)) * Convert.ToDouble(txtNT_lum.Text)).ToString("F4");
            txtNT_2.Text = (Math.Pow((Parttern_NT37700C[31] / 255.0), Convert.ToDouble(txtNT_gamma.Text)) * Convert.ToDouble(txtNT_lum.Text)).ToString("F4");
            txtNT_1.Text = (Math.Pow((Parttern_NT37700C[32] / 255.0), Convert.ToDouble(txtNT_gamma.Text)) * Convert.ToDouble(txtNT_lum.Text)).ToString("F4");
            txtNT_0.Text = (Math.Pow((Parttern_NT37700C[33] / 255.0), Convert.ToDouble(txtNT_gamma.Text)) * Convert.ToDouble(txtNT_lum.Text)).ToString("F4");
        }
        private void btnAutoGammas_NT37700C_Click(object sender, EventArgs e)
        {
            int flag = 1;
            double Lr_d, Lg_d, Lb_d;
            int total_points = 34;

            D_lums = new double[total_points, 3];
            D_lum = new double[total_points];

            index = 0;
            adjust_Rnum = 0;
            adjust_Gnum = 0;
            adjust_Bnum = 0;
            adjust_l = 0;
            adjust_x = 0;
            adjust_y = 0;
            flag1 = 1;
            flag2 = 1;
            flag3 = 1;

            D_lum[0] = Convert.ToDouble(txtNT_33.Text);
            D_lum[1] = Convert.ToDouble(txtNT_32.Text);
            D_lum[2] = Convert.ToDouble(txtNT_31.Text);
            D_lum[3] = Convert.ToDouble(txtNT_30.Text);
            D_lum[4] = Convert.ToDouble(txtNT_29.Text);
            D_lum[5] = Convert.ToDouble(txtNT_28.Text);
            D_lum[6] = Convert.ToDouble(txtNT_27.Text);
            D_lum[7] = Convert.ToDouble(txtNT_26.Text);
            D_lum[8] = Convert.ToDouble(txtNT_25.Text);
            D_lum[9] = Convert.ToDouble(txtNT_24.Text);
            D_lum[10] = Convert.ToDouble(txtNT_23.Text);
            D_lum[11] = Convert.ToDouble(txtNT_22.Text);
            D_lum[12] = Convert.ToDouble(txtNT_21.Text);
            D_lum[13] = Convert.ToDouble(txtNT_20.Text);
            D_lum[14] = Convert.ToDouble(txtNT_19.Text);
            D_lum[15] = Convert.ToDouble(txtNT_18.Text);
            D_lum[16] = Convert.ToDouble(txtNT_17.Text);
            D_lum[17] = Convert.ToDouble(txtNT_16.Text);
            D_lum[18] = Convert.ToDouble(txtNT_15.Text);
            D_lum[19] = Convert.ToDouble(txtNT_14.Text);
            D_lum[20] = Convert.ToDouble(txtNT_13.Text);
            D_lum[21] = Convert.ToDouble(txtNT_12.Text);
            D_lum[22] = Convert.ToDouble(txtNT_11.Text);
            D_lum[23] = Convert.ToDouble(txtNT_10.Text);
            D_lum[24] = Convert.ToDouble(txtNT_9.Text);
            D_lum[25] = Convert.ToDouble(txtNT_8.Text);
            D_lum[26] = Convert.ToDouble(txtNT_7.Text);
            D_lum[27] = Convert.ToDouble(txtNT_6.Text);
            D_lum[28] = Convert.ToDouble(txtNT_5.Text);
            D_lum[29] = Convert.ToDouble(txtNT_4.Text);
            D_lum[30] = Convert.ToDouble(txtNT_3.Text);
            D_lum[31] = Convert.ToDouble(txtNT_2.Text);
            D_lum[32] = Convert.ToDouble(txtNT_1.Text);
            D_lum[33] = Convert.ToDouble(txtNT_0.Text);

            D_x = Convert.ToDouble(txtNT_x.Text);
            D_y = Convert.ToDouble(txtNT_y.Text);
            Lr_d = R_y * ((D_y - G_y) * (B_x - D_x) + (D_x - G_x) * (D_y - B_y)) / B_y / ((D_y - G_y) * (D_x - R_x) + (D_x - G_x) * (R_y - D_y));
            Lb_d = 1.0;
            Lg_d = G_y * ((D_y - R_y) * (B_x - D_x) + (D_x - R_x) * (D_y - B_y)) / B_y / ((D_y - R_y) * (D_x - G_x) + (D_x - R_x) * (G_y - D_y));

            for (int i = 0; i < total_points; i++)
            {
                D_lums[i, 0] = D_lum[i] * Lr_d / (Lr_d + Lg_d + Lb_d);
                D_lums[i, 1] = D_lum[i] * Lg_d / (Lr_d + Lg_d + Lb_d);
                D_lums[i, 2] = D_lum[i] * Lb_d / (Lr_d + Lg_d + Lb_d);
            }

            switch (flag)
            {
                case 1://寄存器值与亮度正比
                    {
                        lum_off = Convert.ToDouble(txtNT_loff.Text);
                        x_off = Convert.ToDouble(txtNT_xoff.Text);
                        y_off = Convert.ToDouble(txtNT_yoff.Text);

                        if (NT37700C_AOD_Inter_cbx.Checked == true)  //AOD gamma adjust in AOD mode or not
                        {
                            //R66451_btnPrevPic_Run("A" + Parttern_NT37700C[index].ToString() + Parttern_NT37700C[index].ToString() + Parttern_NT37700C[index].ToString() + ".bmp");
                            numPatternR.Value = 255; numPatternG.Value = 255; numPatternB.Value = 255;
                            XX0.Value = (int)((int)(H_P.Value) * 20 / 100); XX1.Value = (int)((int)(H_P.Value) * 80 / 100);//Partial display
                            YY0.Value = (int)((int)(V_P.Value) * 35 / 100); YY1.Value = (int)((int)(V_P.Value) * 65 / 100);
                            LCD_Cfg_Click(btnPartial, null);
                        }
                        else
                        {
                            LCD_Cfg_Click(btnwhite, e);
                        }
                        Thread.Sleep(10);
                        T20.Enabled = true;
                    }
                    break;
                case 2:                                           //寄存器值与亮度反比
                    break;
                default:
                    break;
            }
        }
        private void NT37700C_Others_Click(object sender, EventArgs e)
        {
            NT37700C_GAMMA_Click(NT37700C_GammaW_Update, null);
            Thread.Sleep(50);

            Send_buf = new byte[7];
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 7;

            Send_buf[0] = 0x4E;
            Send_buf[2] = 0x02;

            switch (btnTmp.Name)
            {
                case "NT37700C_OTP": //otp Gamma
                    {
                        Send_buf[1] = 0x0D;
                        if (NT37700C_OTP_ALL.Checked == true)
                        { Send_buf[3] = 0xA0; }
                        else if (NT37700C_OTP_P0.Checked == true)
                        { Send_buf[3] = 0x00; }
                        else if (NT37700C_OTP_P1.Checked == true)
                        { Send_buf[3] = 0x01; }
                        else if (NT37700C_OTP_P2.Checked == true)
                        { Send_buf[3] = 0x02; }
                        else if (NT37700C_OTP_P3.Checked == true)
                        { Send_buf[3] = 0x03; }
                        else if (NT37700C_OTP_P4.Checked == true)
                        { Send_buf[3] = 0x04; }
                        else if (NT37700C_OTP_P5.Checked == true)
                        { Send_buf[3] = 0x05; }
                        else if (NT37700C_OTP_P6.Checked == true)
                        { Send_buf[3] = 0x06; }
                        else if (NT37700C_OTP_P7.Checked == true)
                        { Send_buf[3] = 0x07; }
                        else if (NT37700C_OTP_P8.Checked == true)
                        { Send_buf[3] = 0x08; }
                    }
                    break;
                case "NT37700C_QE1":
                    {
                        Send_buf[1] = 0x20;
                    }
                    break;
                case "NT37700C_Erase_Flash":
                    {
                        Send_buf[1] = 0x21;
                    }
                    break;
                case "NT37700C_Program":
                    {
                        Send_buf[1] = 0x22;
                    }
                    break;
                case "NT37700C_Show":
                    {
                        Send_buf[1] = 0x23;
                    }
                    break;
                default:
                    break;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
            try
            {
                comPort.Write(Send_buf, 0, 7);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            T1.Enabled = false;
            T1.Interval = 2000;
            T1.Enabled = true;
        }
        private void NT37700C_GAMMA_Click(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;
            Send_buf = new byte[77];
            Setting.TempRecord.cmdLen = 77;
            Send_buf[2] = 72;
            Send_buf[3] = 0;
            Send_buf[0] = 0x4E; //NT37700

            switch (btnTmp.Name)
            {
                case "NT37700C_GammaR_RED":
                    {
                        Send_buf[1] = 0x09;                             //读Gamma
                        Send_buf[3] = 0x00;                            //红色red
                        Send_buf[4] = (byte)(NT37700C_Gamma_Group.Value);
                        Setting.TempRecord.NT37700CCmdType = 0x03;
                    }
                    break;
                case "NT37700C_GammaR_GREEN":
                    {
                        Send_buf[1] = 0x09;                         //  读Gamma
                        Send_buf[3] = 0x42;                        //绿色 green
                        Send_buf[4] = (byte)(NT37700C_Gamma_Group.Value);
                        Setting.TempRecord.NT37700CCmdType = 0x04;
                    }
                    break;
                case "NT37700C_GammaR_BLUE":
                    {
                        Send_buf[1] = 0x09;                         //   读Gamma
                        Send_buf[3] = 0x81;                        //   blue  
                        Send_buf[4] = (byte)(NT37700C_Gamma_Group.Value);
                        Setting.TempRecord.NT37700CCmdType = 0x05;
                    }
                    break;
                case "NT37700C_GammaW_RED":
                    {
                        Send_buf[1] = 0x08;                       //写Gamma
                        Send_buf[3] = 0x00;                       //红色1
                        Send_buf[4] = (byte)(NT37700C_Gamma_Group.Value);//gamma_group_select

                        Send_buf[5] = Convert.ToByte(Convert.ToUInt16(NT37700C_R0.Value.ToString()) >> 8);
                        Send_buf[6] = Convert.ToByte((Convert.ToUInt16(NT37700C_R0.Value.ToString())) & 0xff);
                        Send_buf[7] = Convert.ToByte(Convert.ToUInt16(NT37700C_R1.Value.ToString()) >> 8);
                        Send_buf[8] = Convert.ToByte((Convert.ToUInt16(NT37700C_R1.Value.ToString())) & 0xff);
                        Send_buf[9] = Convert.ToByte(Convert.ToUInt16(NT37700C_R2.Value.ToString()) >> 8);
                        Send_buf[10] = Convert.ToByte((Convert.ToUInt16(NT37700C_R2.Value.ToString())) & 0xff);
                        Send_buf[11] = Convert.ToByte(Convert.ToUInt16(NT37700C_R3.Value.ToString()) >> 8);
                        Send_buf[12] = Convert.ToByte((Convert.ToUInt16(NT37700C_R3.Value.ToString())) & 0xff);
                        Send_buf[13] = Convert.ToByte(Convert.ToUInt16(NT37700C_R4.Value.ToString()) >> 8);
                        Send_buf[14] = Convert.ToByte((Convert.ToUInt16(NT37700C_R4.Value.ToString())) & 0xff);
                        Send_buf[15] = Convert.ToByte(Convert.ToUInt16(NT37700C_R5.Value.ToString()) >> 8);
                        Send_buf[16] = Convert.ToByte((Convert.ToUInt16(NT37700C_R5.Value.ToString())) & 0xff);
                        Send_buf[17] = Convert.ToByte(Convert.ToUInt16(NT37700C_R6.Value.ToString()) >> 8);
                        Send_buf[18] = Convert.ToByte((Convert.ToUInt16(NT37700C_R6.Value.ToString())) & 0xff);
                        Send_buf[19] = Convert.ToByte(Convert.ToUInt16(NT37700C_R7.Value.ToString()) >> 8);
                        Send_buf[20] = Convert.ToByte((Convert.ToUInt16(NT37700C_R7.Value.ToString())) & 0xff);
                        Send_buf[21] = Convert.ToByte(Convert.ToUInt16(NT37700C_R8.Value.ToString()) >> 8);
                        Send_buf[22] = Convert.ToByte((Convert.ToUInt16(NT37700C_R8.Value.ToString())) & 0xff);
                        Send_buf[23] = Convert.ToByte(Convert.ToUInt16(NT37700C_R9.Value.ToString()) >> 8);
                        Send_buf[24] = Convert.ToByte((Convert.ToUInt16(NT37700C_R9.Value.ToString())) & 0xff);
                        Send_buf[25] = Convert.ToByte(Convert.ToUInt16(NT37700C_R10.Value.ToString()) >> 8);
                        Send_buf[26] = Convert.ToByte((Convert.ToUInt16(NT37700C_R10.Value.ToString())) & 0xff);
                        Send_buf[27] = Convert.ToByte(Convert.ToUInt16(NT37700C_R11.Value.ToString()) >> 8);
                        Send_buf[28] = Convert.ToByte((Convert.ToUInt16(NT37700C_R11.Value.ToString())) & 0xff);

                        Send_buf[29] = Convert.ToByte(Convert.ToUInt16(NT37700C_R12.Value.ToString()) >> 8);
                        Send_buf[30] = Convert.ToByte((Convert.ToUInt16(NT37700C_R12.Value.ToString())) & 0xff);
                        Send_buf[31] = Convert.ToByte(Convert.ToUInt16(NT37700C_R13.Value.ToString()) >> 8);
                        Send_buf[32] = Convert.ToByte((Convert.ToUInt16(NT37700C_R13.Value.ToString())) & 0xff);
                        Send_buf[33] = Convert.ToByte(Convert.ToUInt16(NT37700C_R14.Value.ToString()) >> 8);
                        Send_buf[34] = Convert.ToByte((Convert.ToUInt16(NT37700C_R14.Value.ToString())) & 0xff);
                        Send_buf[35] = Convert.ToByte(Convert.ToUInt16(NT37700C_R15.Value.ToString()) >> 8);
                        Send_buf[36] = Convert.ToByte((Convert.ToUInt16(NT37700C_R15.Value.ToString())) & 0xff);
                        Send_buf[37] = Convert.ToByte(Convert.ToUInt16(NT37700C_R16.Value.ToString()) >> 8);
                        Send_buf[38] = Convert.ToByte((Convert.ToUInt16(NT37700C_R16.Value.ToString())) & 0xff);
                        Send_buf[39] = Convert.ToByte(Convert.ToUInt16(NT37700C_R17.Value.ToString()) >> 8);
                        Send_buf[40] = Convert.ToByte((Convert.ToUInt16(NT37700C_R17.Value.ToString())) & 0xff);
                        Send_buf[41] = Convert.ToByte(Convert.ToUInt16(NT37700C_R18.Value.ToString()) >> 8);
                        Send_buf[42] = Convert.ToByte((Convert.ToUInt16(NT37700C_R18.Value.ToString())) & 0xff);
                        Send_buf[43] = Convert.ToByte(Convert.ToUInt16(NT37700C_R19.Value.ToString()) >> 8);
                        Send_buf[44] = Convert.ToByte((Convert.ToUInt16(NT37700C_R19.Value.ToString())) & 0xff);
                        Send_buf[45] = Convert.ToByte(Convert.ToUInt16(NT37700C_R20.Value.ToString()) >> 8);
                        Send_buf[46] = Convert.ToByte((Convert.ToUInt16(NT37700C_R20.Value.ToString())) & 0xff);
                        Send_buf[47] = Convert.ToByte(Convert.ToUInt16(NT37700C_R21.Value.ToString()) >> 8);
                        Send_buf[48] = Convert.ToByte((Convert.ToUInt16(NT37700C_R21.Value.ToString())) & 0xff);
                        Send_buf[49] = Convert.ToByte(Convert.ToUInt16(NT37700C_R22.Value.ToString()) >> 8);
                        Send_buf[50] = Convert.ToByte((Convert.ToUInt16(NT37700C_R22.Value.ToString())) & 0xff);
                        Send_buf[51] = Convert.ToByte(Convert.ToUInt16(NT37700C_R23.Value.ToString()) >> 8);
                        Send_buf[52] = Convert.ToByte((Convert.ToUInt16(NT37700C_R23.Value.ToString())) & 0xff);

                        Send_buf[53] = Convert.ToByte(Convert.ToUInt16(NT37700C_R24.Value.ToString()) >> 8);
                        Send_buf[54] = Convert.ToByte((Convert.ToUInt16(NT37700C_R24.Value.ToString())) & 0xff);
                        Send_buf[55] = Convert.ToByte(Convert.ToUInt16(NT37700C_R25.Value.ToString()) >> 8);
                        Send_buf[56] = Convert.ToByte((Convert.ToUInt16(NT37700C_R25.Value.ToString())) & 0xff);
                        Send_buf[57] = Convert.ToByte(Convert.ToUInt16(NT37700C_R26.Value.ToString()) >> 8);
                        Send_buf[58] = Convert.ToByte((Convert.ToUInt16(NT37700C_R26.Value.ToString())) & 0xff);
                        Send_buf[59] = Convert.ToByte(Convert.ToUInt16(NT37700C_R27.Value.ToString()) >> 8);
                        Send_buf[60] = Convert.ToByte((Convert.ToUInt16(NT37700C_R27.Value.ToString())) & 0xff);
                        Send_buf[61] = Convert.ToByte(Convert.ToUInt16(NT37700C_R28.Value.ToString()) >> 8);
                        Send_buf[62] = Convert.ToByte((Convert.ToUInt16(NT37700C_R28.Value.ToString())) & 0xff);
                        Send_buf[63] = Convert.ToByte(Convert.ToUInt16(NT37700C_R29.Value.ToString()) >> 8);
                        Send_buf[64] = Convert.ToByte((Convert.ToUInt16(NT37700C_R29.Value.ToString())) & 0xff);
                        Send_buf[65] = Convert.ToByte(Convert.ToUInt16(NT37700C_R30.Value.ToString()) >> 8);
                        Send_buf[66] = Convert.ToByte((Convert.ToUInt16(NT37700C_R30.Value.ToString())) & 0xff);
                        Send_buf[67] = Convert.ToByte(Convert.ToUInt16(NT37700C_R31.Value.ToString()) >> 8);
                        Send_buf[68] = Convert.ToByte((Convert.ToUInt16(NT37700C_R31.Value.ToString())) & 0xff);
                        Send_buf[69] = Convert.ToByte(Convert.ToUInt16(NT37700C_R32.Value.ToString()) >> 8);
                        Send_buf[70] = Convert.ToByte((Convert.ToUInt16(NT37700C_R32.Value.ToString())) & 0xff);
                        Send_buf[71] = Convert.ToByte(Convert.ToUInt16(NT37700C_R33.Value.ToString()) >> 8);
                        Send_buf[72] = Convert.ToByte((Convert.ToUInt16(NT37700C_R33.Value.ToString())) & 0xff);
                        Send_buf[73] = Convert.ToByte(Convert.ToUInt16(NT37700C_R33.Value.ToString()) >> 8);
                        Send_buf[74] = Convert.ToByte((Convert.ToUInt16(NT37700C_R33.Value.ToString())) & 0xff);
                    }
                    break;
                case "NT37700C_GammaW_GREEN":
                    {
                        Send_buf[1] = 0x08;
                        Send_buf[3] = 0x42;                       //绿色1
                        Send_buf[4] = (byte)(NT37700C_Gamma_Group.Value);

                        Send_buf[5] = Convert.ToByte(Convert.ToUInt16(NT37700C_G0.Value.ToString()) >> 8);
                        Send_buf[6] = Convert.ToByte((Convert.ToUInt16(NT37700C_G0.Value.ToString())) & 0xff);
                        Send_buf[7] = Convert.ToByte(Convert.ToUInt16(NT37700C_G1.Value.ToString()) >> 8);
                        Send_buf[8] = Convert.ToByte((Convert.ToUInt16(NT37700C_G1.Value.ToString())) & 0xff);
                        Send_buf[9] = Convert.ToByte(Convert.ToUInt16(NT37700C_G2.Value.ToString()) >> 8);
                        Send_buf[10] = Convert.ToByte((Convert.ToUInt16(NT37700C_G2.Value.ToString())) & 0xff);
                        Send_buf[11] = Convert.ToByte(Convert.ToUInt16(NT37700C_G3.Value.ToString()) >> 8);
                        Send_buf[12] = Convert.ToByte((Convert.ToUInt16(NT37700C_G3.Value.ToString())) & 0xff);
                        Send_buf[13] = Convert.ToByte(Convert.ToUInt16(NT37700C_G4.Value.ToString()) >> 8);
                        Send_buf[14] = Convert.ToByte((Convert.ToUInt16(NT37700C_G4.Value.ToString())) & 0xff);
                        Send_buf[15] = Convert.ToByte(Convert.ToUInt16(NT37700C_G5.Value.ToString()) >> 8);
                        Send_buf[16] = Convert.ToByte((Convert.ToUInt16(NT37700C_G5.Value.ToString())) & 0xff);
                        Send_buf[17] = Convert.ToByte(Convert.ToUInt16(NT37700C_G6.Value.ToString()) >> 8);
                        Send_buf[18] = Convert.ToByte((Convert.ToUInt16(NT37700C_G6.Value.ToString())) & 0xff);
                        Send_buf[19] = Convert.ToByte(Convert.ToUInt16(NT37700C_G7.Value.ToString()) >> 8);
                        Send_buf[20] = Convert.ToByte((Convert.ToUInt16(NT37700C_G7.Value.ToString())) & 0xff);
                        Send_buf[21] = Convert.ToByte(Convert.ToUInt16(NT37700C_G8.Value.ToString()) >> 8);
                        Send_buf[22] = Convert.ToByte((Convert.ToUInt16(NT37700C_G8.Value.ToString())) & 0xff);
                        Send_buf[23] = Convert.ToByte(Convert.ToUInt16(NT37700C_G9.Value.ToString()) >> 8);
                        Send_buf[24] = Convert.ToByte((Convert.ToUInt16(NT37700C_G9.Value.ToString())) & 0xff);
                        Send_buf[25] = Convert.ToByte(Convert.ToUInt16(NT37700C_G10.Value.ToString()) >> 8);
                        Send_buf[26] = Convert.ToByte((Convert.ToUInt16(NT37700C_G10.Value.ToString())) & 0xff);
                        Send_buf[27] = Convert.ToByte(Convert.ToUInt16(NT37700C_G11.Value.ToString()) >> 8);
                        Send_buf[28] = Convert.ToByte((Convert.ToUInt16(NT37700C_G11.Value.ToString())) & 0xff);

                        Send_buf[29] = Convert.ToByte(Convert.ToUInt16(NT37700C_G12.Value.ToString()) >> 8);
                        Send_buf[30] = Convert.ToByte((Convert.ToUInt16(NT37700C_G12.Value.ToString())) & 0xff);
                        Send_buf[31] = Convert.ToByte(Convert.ToUInt16(NT37700C_G13.Value.ToString()) >> 8);
                        Send_buf[32] = Convert.ToByte((Convert.ToUInt16(NT37700C_G13.Value.ToString())) & 0xff);
                        Send_buf[33] = Convert.ToByte(Convert.ToUInt16(NT37700C_G14.Value.ToString()) >> 8);
                        Send_buf[34] = Convert.ToByte((Convert.ToUInt16(NT37700C_G14.Value.ToString())) & 0xff);
                        Send_buf[35] = Convert.ToByte(Convert.ToUInt16(NT37700C_G15.Value.ToString()) >> 8);
                        Send_buf[36] = Convert.ToByte((Convert.ToUInt16(NT37700C_G15.Value.ToString())) & 0xff);
                        Send_buf[37] = Convert.ToByte(Convert.ToUInt16(NT37700C_G16.Value.ToString()) >> 8);
                        Send_buf[38] = Convert.ToByte((Convert.ToUInt16(NT37700C_G16.Value.ToString())) & 0xff);
                        Send_buf[39] = Convert.ToByte(Convert.ToUInt16(NT37700C_G17.Value.ToString()) >> 8);
                        Send_buf[40] = Convert.ToByte((Convert.ToUInt16(NT37700C_G17.Value.ToString())) & 0xff);
                        Send_buf[41] = Convert.ToByte(Convert.ToUInt16(NT37700C_G18.Value.ToString()) >> 8);
                        Send_buf[42] = Convert.ToByte((Convert.ToUInt16(NT37700C_G18.Value.ToString())) & 0xff);
                        Send_buf[43] = Convert.ToByte(Convert.ToUInt16(NT37700C_G19.Value.ToString()) >> 8);
                        Send_buf[44] = Convert.ToByte((Convert.ToUInt16(NT37700C_G19.Value.ToString())) & 0xff);
                        Send_buf[45] = Convert.ToByte(Convert.ToUInt16(NT37700C_G20.Value.ToString()) >> 8);
                        Send_buf[46] = Convert.ToByte((Convert.ToUInt16(NT37700C_G20.Value.ToString())) & 0xff);
                        Send_buf[47] = Convert.ToByte(Convert.ToUInt16(NT37700C_G21.Value.ToString()) >> 8);
                        Send_buf[48] = Convert.ToByte((Convert.ToUInt16(NT37700C_G21.Value.ToString())) & 0xff);
                        Send_buf[49] = Convert.ToByte(Convert.ToUInt16(NT37700C_G22.Value.ToString()) >> 8);
                        Send_buf[50] = Convert.ToByte((Convert.ToUInt16(NT37700C_G22.Value.ToString())) & 0xff);
                        Send_buf[51] = Convert.ToByte(Convert.ToUInt16(NT37700C_G23.Value.ToString()) >> 8);
                        Send_buf[52] = Convert.ToByte((Convert.ToUInt16(NT37700C_G23.Value.ToString())) & 0xff);

                        Send_buf[53] = Convert.ToByte(Convert.ToUInt16(NT37700C_G24.Value.ToString()) >> 8);
                        Send_buf[54] = Convert.ToByte((Convert.ToUInt16(NT37700C_G24.Value.ToString())) & 0xff);
                        Send_buf[55] = Convert.ToByte(Convert.ToUInt16(NT37700C_G25.Value.ToString()) >> 8);
                        Send_buf[56] = Convert.ToByte((Convert.ToUInt16(NT37700C_G25.Value.ToString())) & 0xff);
                        Send_buf[57] = Convert.ToByte(Convert.ToUInt16(NT37700C_G26.Value.ToString()) >> 8);
                        Send_buf[58] = Convert.ToByte((Convert.ToUInt16(NT37700C_G26.Value.ToString())) & 0xff);
                        Send_buf[59] = Convert.ToByte(Convert.ToUInt16(NT37700C_G27.Value.ToString()) >> 8);
                        Send_buf[60] = Convert.ToByte((Convert.ToUInt16(NT37700C_G27.Value.ToString())) & 0xff);
                        Send_buf[61] = Convert.ToByte(Convert.ToUInt16(NT37700C_G28.Value.ToString()) >> 8);
                        Send_buf[62] = Convert.ToByte((Convert.ToUInt16(NT37700C_G28.Value.ToString())) & 0xff);
                        Send_buf[63] = Convert.ToByte(Convert.ToUInt16(NT37700C_G29.Value.ToString()) >> 8);
                        Send_buf[64] = Convert.ToByte((Convert.ToUInt16(NT37700C_G29.Value.ToString())) & 0xff);
                        Send_buf[65] = Convert.ToByte(Convert.ToUInt16(NT37700C_G30.Value.ToString()) >> 8);
                        Send_buf[66] = Convert.ToByte((Convert.ToUInt16(NT37700C_G30.Value.ToString())) & 0xff);
                        Send_buf[67] = Convert.ToByte(Convert.ToUInt16(NT37700C_G31.Value.ToString()) >> 8);
                        Send_buf[68] = Convert.ToByte((Convert.ToUInt16(NT37700C_G31.Value.ToString())) & 0xff);
                        Send_buf[69] = Convert.ToByte(Convert.ToUInt16(NT37700C_G32.Value.ToString()) >> 8);
                        Send_buf[70] = Convert.ToByte((Convert.ToUInt16(NT37700C_G32.Value.ToString())) & 0xff);
                        Send_buf[71] = Convert.ToByte(Convert.ToUInt16(NT37700C_G33.Value.ToString()) >> 8);
                        Send_buf[72] = Convert.ToByte((Convert.ToUInt16(NT37700C_G33.Value.ToString())) & 0xff);
                        Send_buf[73] = Convert.ToByte(Convert.ToUInt16(NT37700C_G33.Value.ToString()) >> 8);
                        Send_buf[74] = Convert.ToByte((Convert.ToUInt16(NT37700C_G33.Value.ToString())) & 0xff);
                    }
                    break;
                case "NT37700C_GammaW_BLUE":     //8
                    {
                        Send_buf[1] = 0x08;
                        Send_buf[3] = 0x81;
                        Send_buf[4] = (byte)(NT37700C_Gamma_Group.Value);

                        Send_buf[5] = Convert.ToByte(Convert.ToUInt16(NT37700C_B0.Value.ToString()) >> 8);
                        Send_buf[6] = Convert.ToByte((Convert.ToUInt16(NT37700C_B0.Value.ToString())) & 0xff);
                        Send_buf[7] = Convert.ToByte(Convert.ToUInt16(NT37700C_B1.Value.ToString()) >> 8);
                        Send_buf[8] = Convert.ToByte((Convert.ToUInt16(NT37700C_B1.Value.ToString())) & 0xff);
                        Send_buf[9] = Convert.ToByte(Convert.ToUInt16(NT37700C_B2.Value.ToString()) >> 8);
                        Send_buf[10] = Convert.ToByte((Convert.ToUInt16(NT37700C_B2.Value.ToString())) & 0xff);
                        Send_buf[11] = Convert.ToByte(Convert.ToUInt16(NT37700C_B3.Value.ToString()) >> 8);
                        Send_buf[12] = Convert.ToByte((Convert.ToUInt16(NT37700C_B3.Value.ToString())) & 0xff);
                        Send_buf[13] = Convert.ToByte(Convert.ToUInt16(NT37700C_B4.Value.ToString()) >> 8);
                        Send_buf[14] = Convert.ToByte((Convert.ToUInt16(NT37700C_B4.Value.ToString())) & 0xff);
                        Send_buf[15] = Convert.ToByte(Convert.ToUInt16(NT37700C_B5.Value.ToString()) >> 8);
                        Send_buf[16] = Convert.ToByte((Convert.ToUInt16(NT37700C_B5.Value.ToString())) & 0xff);
                        Send_buf[17] = Convert.ToByte(Convert.ToUInt16(NT37700C_B6.Value.ToString()) >> 8);
                        Send_buf[18] = Convert.ToByte((Convert.ToUInt16(NT37700C_B6.Value.ToString())) & 0xff);
                        Send_buf[19] = Convert.ToByte(Convert.ToUInt16(NT37700C_B7.Value.ToString()) >> 8);
                        Send_buf[20] = Convert.ToByte((Convert.ToUInt16(NT37700C_B7.Value.ToString())) & 0xff);
                        Send_buf[21] = Convert.ToByte(Convert.ToUInt16(NT37700C_B8.Value.ToString()) >> 8);
                        Send_buf[22] = Convert.ToByte((Convert.ToUInt16(NT37700C_B8.Value.ToString())) & 0xff);
                        Send_buf[23] = Convert.ToByte(Convert.ToUInt16(NT37700C_B9.Value.ToString()) >> 8);
                        Send_buf[24] = Convert.ToByte((Convert.ToUInt16(NT37700C_B9.Value.ToString())) & 0xff);
                        Send_buf[25] = Convert.ToByte(Convert.ToUInt16(NT37700C_B10.Value.ToString()) >> 8);
                        Send_buf[26] = Convert.ToByte((Convert.ToUInt16(NT37700C_B10.Value.ToString())) & 0xff);
                        Send_buf[27] = Convert.ToByte(Convert.ToUInt16(NT37700C_B11.Value.ToString()) >> 8);
                        Send_buf[28] = Convert.ToByte((Convert.ToUInt16(NT37700C_B11.Value.ToString())) & 0xff);

                        Send_buf[29] = Convert.ToByte(Convert.ToUInt16(NT37700C_B12.Value.ToString()) >> 8);
                        Send_buf[30] = Convert.ToByte((Convert.ToUInt16(NT37700C_B12.Value.ToString())) & 0xff);
                        Send_buf[31] = Convert.ToByte(Convert.ToUInt16(NT37700C_B13.Value.ToString()) >> 8);
                        Send_buf[32] = Convert.ToByte((Convert.ToUInt16(NT37700C_B13.Value.ToString())) & 0xff);
                        Send_buf[33] = Convert.ToByte(Convert.ToUInt16(NT37700C_B14.Value.ToString()) >> 8);
                        Send_buf[34] = Convert.ToByte((Convert.ToUInt16(NT37700C_B14.Value.ToString())) & 0xff);
                        Send_buf[35] = Convert.ToByte(Convert.ToUInt16(NT37700C_B15.Value.ToString()) >> 8);
                        Send_buf[36] = Convert.ToByte((Convert.ToUInt16(NT37700C_B15.Value.ToString())) & 0xff);
                        Send_buf[37] = Convert.ToByte(Convert.ToUInt16(NT37700C_B16.Value.ToString()) >> 8);
                        Send_buf[38] = Convert.ToByte((Convert.ToUInt16(NT37700C_B16.Value.ToString())) & 0xff);
                        Send_buf[39] = Convert.ToByte(Convert.ToUInt16(NT37700C_B17.Value.ToString()) >> 8);
                        Send_buf[40] = Convert.ToByte((Convert.ToUInt16(NT37700C_B17.Value.ToString())) & 0xff);
                        Send_buf[41] = Convert.ToByte(Convert.ToUInt16(NT37700C_B18.Value.ToString()) >> 8);
                        Send_buf[42] = Convert.ToByte((Convert.ToUInt16(NT37700C_B18.Value.ToString())) & 0xff);
                        Send_buf[43] = Convert.ToByte(Convert.ToUInt16(NT37700C_B19.Value.ToString()) >> 8);
                        Send_buf[44] = Convert.ToByte((Convert.ToUInt16(NT37700C_B19.Value.ToString())) & 0xff);
                        Send_buf[45] = Convert.ToByte(Convert.ToUInt16(NT37700C_B20.Value.ToString()) >> 8);
                        Send_buf[46] = Convert.ToByte((Convert.ToUInt16(NT37700C_B20.Value.ToString())) & 0xff);
                        Send_buf[47] = Convert.ToByte(Convert.ToUInt16(NT37700C_B21.Value.ToString()) >> 8);
                        Send_buf[48] = Convert.ToByte((Convert.ToUInt16(NT37700C_B21.Value.ToString())) & 0xff);
                        Send_buf[49] = Convert.ToByte(Convert.ToUInt16(NT37700C_B22.Value.ToString()) >> 8);
                        Send_buf[50] = Convert.ToByte((Convert.ToUInt16(NT37700C_B22.Value.ToString())) & 0xff);
                        Send_buf[51] = Convert.ToByte(Convert.ToUInt16(NT37700C_B23.Value.ToString()) >> 8);
                        Send_buf[52] = Convert.ToByte((Convert.ToUInt16(NT37700C_B23.Value.ToString())) & 0xff);

                        Send_buf[53] = Convert.ToByte(Convert.ToUInt16(NT37700C_B24.Value.ToString()) >> 8);
                        Send_buf[54] = Convert.ToByte((Convert.ToUInt16(NT37700C_B24.Value.ToString())) & 0xff);
                        Send_buf[55] = Convert.ToByte(Convert.ToUInt16(NT37700C_B25.Value.ToString()) >> 8);
                        Send_buf[56] = Convert.ToByte((Convert.ToUInt16(NT37700C_B25.Value.ToString())) & 0xff);
                        Send_buf[57] = Convert.ToByte(Convert.ToUInt16(NT37700C_B26.Value.ToString()) >> 8);
                        Send_buf[58] = Convert.ToByte((Convert.ToUInt16(NT37700C_B26.Value.ToString())) & 0xff);
                        Send_buf[59] = Convert.ToByte(Convert.ToUInt16(NT37700C_B27.Value.ToString()) >> 8);
                        Send_buf[60] = Convert.ToByte((Convert.ToUInt16(NT37700C_B27.Value.ToString())) & 0xff);
                        Send_buf[61] = Convert.ToByte(Convert.ToUInt16(NT37700C_B28.Value.ToString()) >> 8);
                        Send_buf[62] = Convert.ToByte((Convert.ToUInt16(NT37700C_B28.Value.ToString())) & 0xff);
                        Send_buf[63] = Convert.ToByte(Convert.ToUInt16(NT37700C_B29.Value.ToString()) >> 8);
                        Send_buf[64] = Convert.ToByte((Convert.ToUInt16(NT37700C_B29.Value.ToString())) & 0xff);
                        Send_buf[65] = Convert.ToByte(Convert.ToUInt16(NT37700C_B30.Value.ToString()) >> 8);
                        Send_buf[66] = Convert.ToByte((Convert.ToUInt16(NT37700C_B30.Value.ToString())) & 0xff);
                        Send_buf[67] = Convert.ToByte(Convert.ToUInt16(NT37700C_B31.Value.ToString()) >> 8);
                        Send_buf[68] = Convert.ToByte((Convert.ToUInt16(NT37700C_B31.Value.ToString())) & 0xff);
                        Send_buf[69] = Convert.ToByte(Convert.ToUInt16(NT37700C_B32.Value.ToString()) >> 8);
                        Send_buf[70] = Convert.ToByte((Convert.ToUInt16(NT37700C_B32.Value.ToString())) & 0xff);
                        Send_buf[71] = Convert.ToByte(Convert.ToUInt16(NT37700C_B33.Value.ToString()) >> 8);
                        Send_buf[72] = Convert.ToByte((Convert.ToUInt16(NT37700C_B33.Value.ToString())) & 0xff);
                        Send_buf[73] = Convert.ToByte(Convert.ToUInt16(NT37700C_B33.Value.ToString()) >> 8);
                        Send_buf[74] = Convert.ToByte((Convert.ToUInt16(NT37700C_B33.Value.ToString())) & 0xff);
                    }
                    break;
                case "NT37700C_GammaW_Update":
                    {
                        Send_buf[1] = 0x03;                       //update Gamma
                        Send_buf[3] = 0x00;                       //红色1
                        Send_buf[4] = (byte)(NT37700C_Gamma_Group.Value);//gamma_group_select
                    }
                    break;
                default:
                    break;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[75], out Send_buf[76]);
            try
            {
                comPort.Write(Send_buf, 0, 77);   //NT37700C
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            T1.Enabled = false;
            T1.Interval = 2000;
            T1.Enabled = true;
        }
        private void T20_Tick(object sender, EventArgs e)
        {
            NT37700C_1D_gamma();
        }
        private void NT37700C_1D_gamma()
        {
            int total_points = 34;
            double Lr_t, Lg_t, Lb_t, lumr, lumg, lumb;

            Msr_CA310();

            if ((auto_lum < (D_lum[index] + lum_off)) && (auto_lum > (D_lum[index] - lum_off)))//G
                flag1 = 0;
            if ((auto_y < (D_y + y_off)) && (auto_y > (D_y - y_off)) && flag1 == 0)//B
                flag2 = 0;
            if ((auto_x < (D_x + x_off)) && (auto_x > (D_x - x_off)) && flag1 == 0 && flag2 == 0)//R
                flag3 = 0;

            if (flag1 == 0 && flag2 == 0 && flag3 == 0)
            {
                T20.Enabled = false;
                //reset-----
                Gamma_adjust_timeout_flag = false;
                Gamma_auto_adjust_count = 0;
                //--------
                index++;
                adjust_Rnum = 0;
                adjust_Gnum = 0;
                adjust_Bnum = 0;
                adjust_l = 0;
                adjust_x = 0;
                adjust_y = 0;
                if (index == total_points)
                {
                    textBox1.Text = textBox1.Text + "NT37700C gamma调试完毕!\r\n";
                }
                if (index < total_points)
                {
                    switch (index)
                    {
                        case 0:
                            lum_off = (double)((Convert.ToDouble(txtNT_33.Text) * Convert.ToDouble(textNT_33_off.Text)) / 100);
                            break;
                        case 1:
                            lum_off = (double)((Convert.ToDouble(txtNT_32.Text) * Convert.ToDouble(textNT_32_off.Text)) / 100);
                            break;
                        case 2:
                            lum_off = (double)((Convert.ToDouble(txtNT_31.Text) * Convert.ToDouble(textNT_31_off.Text)) / 100);
                            break;
                        case 3:
                            lum_off = (double)((Convert.ToDouble(txtNT_30.Text) * Convert.ToDouble(textNT_30_off.Text)) / 100);
                            break;
                        case 4:
                            lum_off = (double)((Convert.ToDouble(txtNT_29.Text) * Convert.ToDouble(textNT_29_off.Text)) / 100);
                            break;
                        case 5:
                            lum_off = (double)((Convert.ToDouble(txtNT_28.Text) * Convert.ToDouble(textNT_28_off.Text)) / 100);
                            break;
                        case 6:
                            lum_off = (double)((Convert.ToDouble(txtNT_27.Text) * Convert.ToDouble(textNT_27_off.Text)) / 100);
                            break;
                        case 7:
                            lum_off = (double)((Convert.ToDouble(txtNT_26.Text) * Convert.ToDouble(textNT_26_off.Text)) / 100);
                            break;
                        case 8:
                            lum_off = (double)((Convert.ToDouble(txtNT_25.Text) * Convert.ToDouble(textNT_25_off.Text)) / 100);
                            break;
                        case 9:
                            lum_off = (double)((Convert.ToDouble(txtNT_24.Text) * Convert.ToDouble(textNT_24_off.Text)) / 100);
                            break;
                        case 10:
                            lum_off = (double)((Convert.ToDouble(txtNT_23.Text) * Convert.ToDouble(textNT_23_off.Text)) / 100);
                            break;
                        case 11:
                            lum_off = (double)((Convert.ToDouble(txtNT_22.Text) * Convert.ToDouble(textNT_22_off.Text)) / 100);
                            break;
                        case 12:
                            lum_off = (double)((Convert.ToDouble(txtNT_21.Text) * Convert.ToDouble(textNT_21_off.Text)) / 100);
                            break;
                        case 13:
                            lum_off = (double)((Convert.ToDouble(txtNT_20.Text) * Convert.ToDouble(textNT_20_off.Text)) / 100);
                            break;
                        case 14:
                            lum_off = (double)((Convert.ToDouble(txtNT_19.Text) * Convert.ToDouble(textNT_19_off.Text)) / 100);
                            break;
                        case 15:
                            lum_off = (double)((Convert.ToDouble(txtNT_18.Text) * Convert.ToDouble(textNT_18_off.Text)) / 100);
                            break;
                        case 16:
                            lum_off = (double)((Convert.ToDouble(txtNT_17.Text) * Convert.ToDouble(textNT_17_off.Text)) / 100);
                            break;
                        case 17:
                            lum_off = (double)((Convert.ToDouble(txtNT_16.Text) * Convert.ToDouble(textNT_16_off.Text)) / 100);
                            break;
                        case 18:
                            lum_off = (double)((Convert.ToDouble(txtNT_15.Text) * Convert.ToDouble(textNT_15_off.Text)) / 100);
                            break;
                        case 19:
                            lum_off = (double)((Convert.ToDouble(txtNT_14.Text) * Convert.ToDouble(textNT_14_off.Text)) / 100);
                            break;
                        case 20:
                            lum_off = (double)((Convert.ToDouble(txtNT_13.Text) * Convert.ToDouble(textNT_13_off.Text)) / 100);
                            break;
                        case 21:
                            lum_off = (double)((Convert.ToDouble(txtNT_12.Text) * Convert.ToDouble(textNT_12_off.Text)) / 100);
                            break;
                        case 22:
                            lum_off = (double)((Convert.ToDouble(txtNT_11.Text) * Convert.ToDouble(textNT_11_off.Text)) / 100);
                            break;
                        case 23:
                            lum_off = (double)((Convert.ToDouble(txtNT_10.Text) * Convert.ToDouble(textNT_10_off.Text)) / 100);
                            break;
                        case 24:
                            lum_off = (double)((Convert.ToDouble(txtNT_9.Text) * Convert.ToDouble(textNT_9_off.Text)) / 100);
                            break;
                        case 25:
                            lum_off = (double)((Convert.ToDouble(txtNT_8.Text) * Convert.ToDouble(textNT_8_off.Text)) / 100);
                            break;
                        case 26:
                            lum_off = (double)((Convert.ToDouble(txtNT_7.Text) * Convert.ToDouble(textNT_7_off.Text)) / 100);
                            break;
                        case 27:
                            lum_off = (double)((Convert.ToDouble(txtNT_6.Text) * Convert.ToDouble(textNT_6_off.Text)) / 100);
                            break;
                        case 28:
                            lum_off = (double)((Convert.ToDouble(txtNT_5.Text) * Convert.ToDouble(textNT_5_off.Text)) / 100);
                            break;
                        case 29:
                            lum_off = (double)((Convert.ToDouble(txtNT_4.Text) * Convert.ToDouble(textNT_4_off.Text)) / 100);
                            break;
                        case 30:
                            lum_off = (double)((Convert.ToDouble(txtNT_3.Text) * Convert.ToDouble(textNT_3_off.Text)) / 100);
                            break;
                        case 31:
                            lum_off = (double)((Convert.ToDouble(txtNT_2.Text) * Convert.ToDouble(textNT_2_off.Text)) / 100);
                            break;
                        case 32:
                            lum_off = (double)((Convert.ToDouble(txtNT_1.Text) * Convert.ToDouble(textNT_1_off.Text)) / 100);
                            break;
                        case 33:
                            lum_off = (double)((Convert.ToDouble(txtNT_0.Text) * Convert.ToDouble(textNT_0_off.Text)) / 100);
                            break;
                    }

                    numPatternR.Value = Parttern_NT37700C[index];//固定255灰阶
                    numPatternG.Value = Parttern_NT37700C[index];
                    numPatternB.Value = Parttern_NT37700C[index];

                    if (NT37700C_AOD_Inter_cbx.Checked == true)  //AOD gamma adjust in AOD mode or not
                    {
                        XX0.Value = (int)((int)(H_P.Value) * 20 / 100); XX1.Value = (int)((int)(H_P.Value) * 80 / 100);//Partial display
                        YY0.Value = (int)((int)(V_P.Value) * 35 / 100); YY1.Value = (int)((int)(V_P.Value) * 65 / 100);
                        LCD_Cfg_Click(btnPartial, null);
                    }
                    else
                    {
                        LCD_Cfg_Click(btnPatternW, null);//写入Pattern参数
                    }
                    Thread.Sleep(10); //1秒=1000毫秒，
                    T20.Enabled = true;
                    flag1 = 1;
                    flag2 = 1;
                    flag3 = 1;
                }
            }
            else
            {
                decimal r = 0, g = 0, b = 0;
                int flexible_gamma_count = 70;

                Lr_t = R_y * ((auto_y - G_y) * (B_x - auto_x) + (auto_x - G_x) * (auto_y - B_y)) / B_y / ((auto_y - G_y) * (auto_x - R_x) + (auto_x - G_x) * (R_y - auto_y));
                Lb_t = 1.0;
                Lg_t = G_y * ((auto_y - R_y) * (B_x - auto_x) + (auto_x - R_x) * (auto_y - B_y)) / B_y / ((auto_y - R_y) * (auto_x - G_x) + (auto_x - R_x) * (G_y - auto_y));
                lumr = auto_lum * Lr_t / (Lr_t + Lg_t + Lb_t);
                lumg = auto_lum * Lg_t / (Lr_t + Lg_t + Lb_t);
                lumb = auto_lum * Lb_t / (Lr_t + Lg_t + Lb_t);

                try
                {
                    switch (index)
                    {
                        case 0:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 30, 3, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NT37700C_R33.Value, NT37700C_G33.Value, NT37700C_B33.Value, Parttern_NT37700C[index], Parttern_NT37700C[index], Parttern_NT37700C[index], 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R33.Value = r;
                                NT37700C_G33.Value = g;
                                NT37700C_B33.Value = b;

                                if (NT37700C_G33.Value <= NT37700C_G32.Value)
                                {
                                    NT37700C_R32.Value = r - 20; NT37700C_G32.Value = g - 20; NT37700C_B32.Value = b - 20;
                                }
                                // auto_gamma_write(gamma_r[index], Convert.ToUInt16(RM692AX_R32.Value), gamma_g[index], Convert.ToUInt16(RM692AX_G32.Value), gamma_b[index], Convert.ToUInt16(RM692AX_B32.Value));
                            }
                            break;
                        case 1:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 30, 3, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NT37700C_R32.Value, NT37700C_G32.Value, NT37700C_B32.Value, Parttern_NT37700C[index], Parttern_NT37700C[index], Parttern_NT37700C[index], 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R32.Value = r;
                                NT37700C_G32.Value = g;
                                NT37700C_B32.Value = b;

                                if (NT37700C_G32.Value <= NT37700C_G31.Value)
                                {
                                    NT37700C_R31.Value = r - 20; NT37700C_G31.Value = g - 20; NT37700C_B31.Value = b - 20;
                                }
                            }
                            break;
                        case 2:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 30, 3, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NT37700C_R31.Value, NT37700C_G31.Value, NT37700C_B31.Value, Parttern_NT37700C[index], Parttern_NT37700C[index], Parttern_NT37700C[index], 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R31.Value = r;
                                NT37700C_G31.Value = g;
                                NT37700C_B31.Value = b;

                                if (NT37700C_G31.Value <= NT37700C_G30.Value)
                                {
                                    NT37700C_R30.Value = r - 20; NT37700C_G30.Value = g - 20; NT37700C_B30.Value = b - 20;
                                }
                            }
                            break;

                        case 3:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 30, 3, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NT37700C_R30.Value, NT37700C_G30.Value, NT37700C_B30.Value, Parttern_NT37700C[index], Parttern_NT37700C[index], Parttern_NT37700C[index], 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R30.Value = r;
                                NT37700C_G30.Value = g;
                                NT37700C_B30.Value = b;

                                if (NT37700C_G30.Value <= NT37700C_G29.Value)
                                {
                                    NT37700C_R29.Value = r - 20; NT37700C_G29.Value = g - 20; NT37700C_B29.Value = b - 20;
                                }
                            }
                            break;

                        case 4:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 30, 3, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NT37700C_R29.Value, NT37700C_G29.Value, NT37700C_B29.Value, Parttern_NT37700C[index], Parttern_NT37700C[index], Parttern_NT37700C[index], 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R29.Value = r;
                                NT37700C_G29.Value = g;
                                NT37700C_B29.Value = b;

                                if (NT37700C_G29.Value <= NT37700C_G28.Value)
                                {
                                    NT37700C_R28.Value = r - 20; NT37700C_G28.Value = g - 20; NT37700C_B28.Value = b - 20;
                                }
                            }
                            break;

                        case 5:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 30, 3, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NT37700C_R28.Value, NT37700C_G28.Value, NT37700C_B28.Value, Parttern_NT37700C[index], Parttern_NT37700C[index], Parttern_NT37700C[index], 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R28.Value = r;
                                NT37700C_G28.Value = g;
                                NT37700C_B28.Value = b;

                                if (NT37700C_G28.Value <= NT37700C_G27.Value)
                                {
                                    NT37700C_R27.Value = r - 20; NT37700C_G27.Value = g - 20; NT37700C_B27.Value = b - 20;
                                }
                            }
                            break;

                        case 6:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 30, 3, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NT37700C_R27.Value, NT37700C_G27.Value, NT37700C_B27.Value, Parttern_NT37700C[index], Parttern_NT37700C[index], Parttern_NT37700C[index], 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R27.Value = r;
                                NT37700C_G27.Value = g;
                                NT37700C_B27.Value = b;

                                if (NT37700C_G27.Value <= NT37700C_G26.Value)
                                {
                                    NT37700C_R26.Value = r - 20; NT37700C_G26.Value = g - 20; NT37700C_B26.Value = b - 20;
                                }
                            }
                            break;

                        case 7:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 30, 3, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NT37700C_R26.Value, NT37700C_G26.Value, NT37700C_B26.Value, Parttern_NT37700C[index], Parttern_NT37700C[index], Parttern_NT37700C[index], 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R26.Value = r;
                                NT37700C_G26.Value = g;
                                NT37700C_B26.Value = b;

                                if (NT37700C_G26.Value <= NT37700C_G25.Value)
                                {
                                    NT37700C_R25.Value = r - 20; NT37700C_G25.Value = g - 20; NT37700C_B25.Value = b - 20;
                                }
                            }
                            break;

                        case 8:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 30, 3, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NT37700C_R25.Value, NT37700C_G25.Value, NT37700C_B25.Value, Parttern_NT37700C[index], Parttern_NT37700C[index], Parttern_NT37700C[index], 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R25.Value = r;
                                NT37700C_G25.Value = g;
                                NT37700C_B25.Value = b;

                                if (NT37700C_G25.Value <= NT37700C_G24.Value)
                                {
                                    NT37700C_R24.Value = r - 20; NT37700C_G24.Value = g - 20; NT37700C_B24.Value = b - 20;
                                }
                            }
                            break;

                        case 9:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 30, 3, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NT37700C_R24.Value, NT37700C_G24.Value, NT37700C_B24.Value, Parttern_NT37700C[index], Parttern_NT37700C[index], Parttern_NT37700C[index], 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R24.Value = r;
                                NT37700C_G24.Value = g;
                                NT37700C_B24.Value = b;

                                if (NT37700C_G24.Value <= NT37700C_G23.Value)
                                {
                                    NT37700C_R23.Value = r - 20; NT37700C_G23.Value = g - 20; NT37700C_B23.Value = b - 20;
                                }
                            }
                            break;

                        case 10:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 30, 3, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NT37700C_R23.Value, NT37700C_G23.Value, NT37700C_B23.Value, Parttern_NT37700C[index], Parttern_NT37700C[index], Parttern_NT37700C[index], 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R23.Value = r;
                                NT37700C_G23.Value = g;
                                NT37700C_B23.Value = b;

                                if (NT37700C_G23.Value <= NT37700C_G22.Value)
                                {
                                    NT37700C_R22.Value = r - 20; NT37700C_G22.Value = g - 20; NT37700C_B22.Value = b - 20;
                                }
                            }
                            break;

                        case 11:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 30, 3, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NT37700C_R22.Value, NT37700C_G22.Value, NT37700C_B22.Value, Parttern_NT37700C[index], Parttern_NT37700C[index], Parttern_NT37700C[index], 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R22.Value = r;
                                NT37700C_G22.Value = g;
                                NT37700C_B22.Value = b;

                                if (NT37700C_G22.Value <= NT37700C_G21.Value)
                                {
                                    NT37700C_R21.Value = r - 20; NT37700C_G21.Value = g - 20; NT37700C_B21.Value = b - 20;
                                }
                            }
                            break;

                        case 12:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 30, 3, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NT37700C_R21.Value, NT37700C_G21.Value, NT37700C_B21.Value, Parttern_NT37700C[index], Parttern_NT37700C[index], Parttern_NT37700C[index], 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R21.Value = r;
                                NT37700C_G21.Value = g;
                                NT37700C_B21.Value = b;

                                if (NT37700C_G21.Value <= NT37700C_G20.Value)
                                {
                                    NT37700C_R20.Value = r - 20; NT37700C_G20.Value = g - 20; NT37700C_B20.Value = b - 20;
                                }
                            }
                            break;
                        case 13:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 30, 3, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NT37700C_R20.Value, NT37700C_G20.Value, NT37700C_B20.Value, Parttern_NT37700C[index], Parttern_NT37700C[index], Parttern_NT37700C[index], 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R20.Value = r;
                                NT37700C_G20.Value = g;
                                NT37700C_B20.Value = b;

                                if (NT37700C_G20.Value <= NT37700C_G19.Value)
                                {
                                    NT37700C_R19.Value = r - 20; NT37700C_G19.Value = g - 20; NT37700C_B19.Value = b - 20;
                                }
                            }
                            break;
                        case 14:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 30, 3, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NT37700C_R19.Value, NT37700C_G19.Value, NT37700C_B19.Value, Parttern_NT37700C[index], Parttern_NT37700C[index], Parttern_NT37700C[index], 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R19.Value = r;
                                NT37700C_G19.Value = g;
                                NT37700C_B19.Value = b;

                                if (NT37700C_G19.Value <= NT37700C_G18.Value)
                                {
                                    NT37700C_R18.Value = r - 20; NT37700C_G18.Value = g - 20; NT37700C_B18.Value = b - 20;
                                }
                            }
                            break;
                        case 15:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 20, 2, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NT37700C_R18.Value, NT37700C_G18.Value, NT37700C_B18.Value, Parttern_NT37700C[index], Parttern_NT37700C[index], Parttern_NT37700C[index], 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R18.Value = r;
                                NT37700C_G18.Value = g;
                                NT37700C_B18.Value = b;
                                if (NT37700C_G18.Value <= NT37700C_G17.Value)
                                {
                                    NT37700C_R17.Value = r - 20; NT37700C_G17.Value = g - 20; NT37700C_B17.Value = b - 20;
                                }
                            }
                            break;
                        case 16:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 20, 2, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NT37700C_R17.Value, NT37700C_G17.Value, NT37700C_B17.Value, Parttern_NT37700C[index], Parttern_NT37700C[index], Parttern_NT37700C[index], 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R17.Value = r;
                                NT37700C_G17.Value = g;
                                NT37700C_B17.Value = b;

                                if (NT37700C_G17.Value <= NT37700C_G16.Value)
                                {
                                    NT37700C_R16.Value = r - 20; NT37700C_G16.Value = g - 20; NT37700C_B16.Value = b - 20;
                                }
                            }
                            break;
                        case 17:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 20, 2, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NT37700C_R16.Value, NT37700C_G16.Value, NT37700C_B16.Value, Parttern_NT37700C[index], Parttern_NT37700C[index], Parttern_NT37700C[index], 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R16.Value = r;
                                NT37700C_G16.Value = g;
                                NT37700C_B16.Value = b;

                                if (NT37700C_G16.Value <= NT37700C_G15.Value)
                                {
                                    NT37700C_R15.Value = r - 20; NT37700C_G15.Value = g - 20; NT37700C_B15.Value = b - 20;
                                }
                            }
                            break;
                        case 18:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 20, 2, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NT37700C_R15.Value, NT37700C_G15.Value, NT37700C_B15.Value, Parttern_NT37700C[index], Parttern_NT37700C[index], Parttern_NT37700C[index], 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R15.Value = r;
                                NT37700C_G15.Value = g;
                                NT37700C_B15.Value = b;

                                if (NT37700C_G15.Value <= NT37700C_G14.Value)
                                {
                                    NT37700C_R14.Value = r - 20; NT37700C_G14.Value = g - 20; NT37700C_B14.Value = b - 20;
                                }
                            }
                            break;
                        case 19:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 20, 2, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NT37700C_R14.Value, NT37700C_G14.Value, NT37700C_B14.Value, Parttern_NT37700C[index], Parttern_NT37700C[index], Parttern_NT37700C[index], 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R14.Value = r;
                                NT37700C_G14.Value = g;
                                NT37700C_B14.Value = b;

                                if (NT37700C_G14.Value <= NT37700C_G13.Value)
                                {
                                    NT37700C_R13.Value = r - 20; NT37700C_G13.Value = g - 20; NT37700C_B13.Value = b - 20;
                                }
                            }
                            break;
                        case 20:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 20, 2, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NT37700C_R13.Value, NT37700C_G13.Value, NT37700C_B13.Value, Parttern_NT37700C[index], Parttern_NT37700C[index], Parttern_NT37700C[index], 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R13.Value = r;
                                NT37700C_G13.Value = g;
                                NT37700C_B13.Value = b;

                                if (NT37700C_G13.Value <= NT37700C_G12.Value)
                                {
                                    NT37700C_R12.Value = r - 20; NT37700C_G12.Value = g - 20; NT37700C_B12.Value = b - 20;
                                }
                            }
                            break;
                        case 21:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 10, 2, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NT37700C_R12.Value, NT37700C_G12.Value, NT37700C_B12.Value, Parttern_NT37700C[index], Parttern_NT37700C[index], Parttern_NT37700C[index], 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R12.Value = r;
                                NT37700C_G12.Value = g;
                                NT37700C_B12.Value = b;

                                if (NT37700C_G12.Value <= NT37700C_G11.Value)
                                {
                                    NT37700C_R11.Value = r - 20; NT37700C_G11.Value = g - 20; NT37700C_B11.Value = b - 20;
                                }
                            }
                            break;
                        case 22:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 10, 2, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NT37700C_R11.Value, NT37700C_G11.Value, NT37700C_B11.Value, Parttern_NT37700C[index], Parttern_NT37700C[index], Parttern_NT37700C[index], 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R11.Value = r;
                                NT37700C_G11.Value = g;
                                NT37700C_B11.Value = b;

                                if (NT37700C_G11.Value <= NT37700C_G10.Value)
                                {
                                    NT37700C_R10.Value = r - 20; NT37700C_G10.Value = g - 20; NT37700C_B10.Value = b - 20;
                                }
                            }
                            break;
                        case 23:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 10, 2, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NT37700C_R10.Value, NT37700C_G10.Value, NT37700C_B10.Value, Parttern_NT37700C[index], Parttern_NT37700C[index], Parttern_NT37700C[index], 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R10.Value = r;
                                NT37700C_G10.Value = g;
                                NT37700C_B10.Value = b;

                                if (NT37700C_G10.Value <= NT37700C_G9.Value)
                                {
                                    NT37700C_R9.Value = r - 20; NT37700C_G9.Value = g - 20; NT37700C_B9.Value = b - 20;
                                }
                            }
                            break;
                        case 24:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 10, 2, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NT37700C_R9.Value, NT37700C_G9.Value, NT37700C_B9.Value, Parttern_NT37700C[index], Parttern_NT37700C[index], Parttern_NT37700C[index], 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R9.Value = r;
                                NT37700C_G9.Value = g;
                                NT37700C_B9.Value = b;

                                if (NT37700C_G9.Value <= NT37700C_G8.Value)
                                {
                                    NT37700C_R8.Value = r - 20; NT37700C_G8.Value = g - 20; NT37700C_B8.Value = b - 20;
                                }
                            }
                            break;
                        case 25:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 10, 2, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NT37700C_R8.Value, NT37700C_G8.Value, NT37700C_B8.Value, Parttern_NT37700C[index], Parttern_NT37700C[index], Parttern_NT37700C[index], 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R8.Value = r;
                                NT37700C_G8.Value = g;
                                NT37700C_B8.Value = b;

                                if (NT37700C_G8.Value <= NT37700C_G7.Value)
                                {
                                    NT37700C_R7.Value = r - 20; NT37700C_G7.Value = g - 20; NT37700C_B7.Value = b - 20;
                                }
                            }
                            break;
                        case 26:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 10, 2, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NT37700C_R7.Value, NT37700C_G7.Value, NT37700C_B7.Value, Parttern_NT37700C[index], Parttern_NT37700C[index], Parttern_NT37700C[index], 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R7.Value = r;
                                NT37700C_G7.Value = g;
                                NT37700C_B7.Value = b;

                                if (NT37700C_G7.Value <= NT37700C_G6.Value)
                                {
                                    NT37700C_R6.Value = r - 20; NT37700C_G6.Value = g - 20; NT37700C_B6.Value = b - 20;
                                }
                            }
                            break;
                        case 27:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 10, 2, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NT37700C_R6.Value, NT37700C_G6.Value, NT37700C_B6.Value, Parttern_NT37700C[index], Parttern_NT37700C[index], Parttern_NT37700C[index], 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R6.Value = r;
                                NT37700C_G6.Value = g;
                                NT37700C_B6.Value = b;

                                if (NT37700C_G6.Value <= NT37700C_G5.Value)
                                {
                                    NT37700C_R5.Value = r - 20; NT37700C_G5.Value = g - 20; NT37700C_B5.Value = b - 20;
                                }
                            }
                            break;
                        case 28:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 10, 2, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NT37700C_R5.Value, NT37700C_G5.Value, NT37700C_B5.Value, Parttern_NT37700C[index], Parttern_NT37700C[index], Parttern_NT37700C[index], 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R5.Value = r;
                                NT37700C_G5.Value = g;
                                NT37700C_B5.Value = b;

                                if (NT37700C_G5.Value <= NT37700C_G4.Value)
                                {
                                    NT37700C_R4.Value = r - 20; NT37700C_G4.Value = g - 20; NT37700C_B4.Value = b - 20;
                                }
                            }
                            break;
                        case 29:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 10, 2, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NT37700C_R4.Value, NT37700C_G4.Value, NT37700C_B4.Value, Parttern_NT37700C[index], Parttern_NT37700C[index], Parttern_NT37700C[index], 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R4.Value = r;
                                NT37700C_G4.Value = g;
                                NT37700C_B4.Value = b;

                                if (NT37700C_G4.Value <= NT37700C_G3.Value)
                                {
                                    NT37700C_R3.Value = r - 10; NT37700C_G3.Value = g - 10; NT37700C_B3.Value = b - 10;
                                }
                            }
                            break;
                        case 30:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 10, 2, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NT37700C_R3.Value, NT37700C_G3.Value, NT37700C_B3.Value, Parttern_NT37700C[index], Parttern_NT37700C[index], Parttern_NT37700C[index], 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R3.Value = r;
                                NT37700C_G3.Value = g;
                                NT37700C_B3.Value = b;

                                if (NT37700C_G3.Value <= NT37700C_G2.Value)
                                {
                                    NT37700C_R2.Value = r - 5; NT37700C_G2.Value = g - 5; NT37700C_B2.Value = b - 5;
                                }
                            }
                            break;
                        case 31:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 10, 2, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NT37700C_R2.Value, NT37700C_G2.Value, NT37700C_B2.Value, Parttern_NT37700C[index], Parttern_NT37700C[index], Parttern_NT37700C[index], 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R2.Value = r;
                                NT37700C_G2.Value = g;
                                NT37700C_B2.Value = b;

                                if (NT37700C_G2.Value <= NT37700C_G1.Value)
                                {
                                    NT37700C_R1.Value = r - 5; NT37700C_G1.Value = g - 5; NT37700C_B1.Value = b - 5;
                                }
                            }
                            break;
                        case 32:
                            {
                                Gamma_adjust_timeout_flag = true;
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT37700C_R1.Value = NT37700C_R2.Value - 0x10;
                                NT37700C_G1.Value = NT37700C_G2.Value - 0x10;
                                NT37700C_B1.Value = NT37700C_B2.Value - 0x10;
                            }
                            break;
                        case 33:
                            {
                                Gamma_adjust_timeout_flag = true;
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                try
                                {
                                    NT37700C_R0.Value = NT37700C_R1.Value - 0xfe;
                                }
                                catch
                                {
                                    NT37700C_R0.Value = 0;
                                }
                                try
                                {
                                    NT37700C_G0.Value = NT37700C_G1.Value - 0xfe;
                                }
                                catch
                                {
                                    NT37700C_G0.Value = 0;
                                }
                                try
                                {
                                    NT37700C_B0.Value = NT37700C_B1.Value - 0xfe;
                                }
                                catch
                                {
                                    NT37700C_B0.Value = 0;
                                }
                            }
                            break;
                        default:
                            break;
                    }
                    //   if (index < 13)
                    //   {
                    //Target_VTDR6100_Lum[index] = auto_lum;
                    //Target_VTDR6100_X[index] = auto_x;
                    //Target_VTDR6100_Y[index] = auto_y;
                    //  }
                    NT37700C_GAMMA_Click(NT37700C_GammaW_RED, null);
                    Thread.Sleep(20);
                    NT37700C_GAMMA_Click(NT37700C_GammaW_GREEN, null);
                    Thread.Sleep(20);
                    NT37700C_GAMMA_Click(NT37700C_GammaW_BLUE, null);
                }
                catch
                {
                    T20.Enabled = false;
                    MessageBox.Show("调整数值超限");
                }
            }
        }
        private void NT37700C_Stop_Click(object sender, EventArgs e)
        {
            T20.Enabled = false;
            flag1 = 1;
            flag2 = 1;
            flag3 = 1;
            adjust_Rnum = 0;
            adjust_Gnum = 0;
            adjust_Bnum = 0;
            adjust_l = 0;
            adjust_x = 0;
            adjust_y = 0;
        }

        private void NT37700C_Gamma_Print_Click(object sender, EventArgs e)
        {

        }

    }
}
